using System;
using System.Drawing;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using AleaClasses;
using AleaPlayer;

namespace AleaDesigner
{
    public delegate void SimpleDelegate();

	/// <summary>
	/// Summary description for TfrmPlayer.
	/// </summary>
	public class TfrmPlayer : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button btnPlay;
		private System.Windows.Forms.TextBox edMessages;
		private System.Windows.Forms.Button btnStop;
		private System.Windows.Forms.ListView lvDriveLabels;
		private System.Windows.Forms.ColumnHeader Labels;
		private System.ComponentModel.IContainer components;

		public TfrmPlayer()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.btnPlay = new System.Windows.Forms.Button();
            this.edMessages = new System.Windows.Forms.TextBox();
            this.btnStop = new System.Windows.Forms.Button();
            this.lvDriveLabels = new System.Windows.Forms.ListView();
            this.Labels = new System.Windows.Forms.ColumnHeader();
            this.SuspendLayout();
            // 
            // btnPlay
            // 
            this.btnPlay.Location = new System.Drawing.Point(8, 8);
            this.btnPlay.Name = "btnPlay";
            this.btnPlay.Size = new System.Drawing.Size(56, 23);
            this.btnPlay.TabIndex = 0;
            this.btnPlay.Text = "Play";
            this.btnPlay.Click += new System.EventHandler(this.btnPlay_Click);
            // 
            // edMessages
            // 
            this.edMessages.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.edMessages.Location = new System.Drawing.Point(136, 40);
            this.edMessages.Multiline = true;
            this.edMessages.Name = "edMessages";
            this.edMessages.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.edMessages.Size = new System.Drawing.Size(328, 324);
            this.edMessages.TabIndex = 1;
            // 
            // btnStop
            // 
            this.btnStop.Location = new System.Drawing.Point(72, 8);
            this.btnStop.Name = "btnStop";
            this.btnStop.Size = new System.Drawing.Size(56, 23);
            this.btnStop.TabIndex = 2;
            this.btnStop.Text = "Stop";
            this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
            // 
            // lvDriveLabels
            // 
            this.lvDriveLabels.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.Labels});
            this.lvDriveLabels.Location = new System.Drawing.Point(8, 40);
            this.lvDriveLabels.Name = "lvDriveLabels";
            this.lvDriveLabels.Size = new System.Drawing.Size(121, 320);
            this.lvDriveLabels.TabIndex = 3;
            this.lvDriveLabels.UseCompatibleStateImageBehavior = false;
            this.lvDriveLabels.View = System.Windows.Forms.View.Details;
            this.lvDriveLabels.SelectedIndexChanged += new System.EventHandler(this.lvDriveLabels_SelectedIndexChanged);
            // 
            // Labels
            // 
            this.Labels.Text = "Labels";
            this.Labels.Width = 112;
            // 
            // TfrmPlayer
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(480, 373);
            this.Controls.Add(this.lvDriveLabels);
            this.Controls.Add(this.btnStop);
            this.Controls.Add(this.edMessages);
            this.Controls.Add(this.btnPlay);
            this.Name = "TfrmPlayer";
            this.Text = "TfrmPlayer";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.TfrmPlayer_FormClosing);
            this.Load += new System.EventHandler(this.TfrmPlayer_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		public TAleaGraph AleaGraph = null;
		TAleaPlayer AleaPlayer = null;
		List<String> fMessages = new List<String>();

        private void PopulateDriveLabelsList()
        {
            lvDriveLabels.Items.Clear();
            foreach (TLabel lLabel in AleaGraph.Labels)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = lLabel.DisplayName;
                lvi.Tag = lLabel;
                lvDriveLabels.Items.Add(lvi);
            }
        }

		private void btnPlay_Click(object sender, System.EventArgs e)
		{
            if (AleaPlayer == null)
            {
                AleaPlayer = new TAleaPlayer(AleaGraph);
                AleaPlayer.AleaPlayerMessageDelegate =
                    new TAleaPlayerMessageDelegate(OnPlayerMessage);
                AleaPlayer.StartPlaying();
            }
		}

		private List<string> GetDriveLabels()
		{
			List<String> retval = new List<String>();

			foreach (ListViewItem lvi in lvDriveLabels.SelectedItems)
			{
				if ( lvi.Tag is TLabel)
				{
					retval.Add((lvi.Tag as TLabel).LabelCode);
				}
			}

			return retval;
		}

		private void UpdateDriveLabels(List<String> aDriveLabels)
		{
			foreach (ListViewItem lvi in lvDriveLabels.Items)
			{
				TLabel lLabel = (lvi.Tag as TLabel);

				if (lLabel != null)
				{
					lvi.Selected = aDriveLabels.Contains(lLabel.LabelCode);
				}
			}
		}

		public void OnPlayerMessage(string aMessage)
		{
            fMessages.Add(aMessage);
            Invoke(new SimpleDelegate(UpdateControlsDuringPlay));
		}

		private void btnStop_Click(object sender, System.EventArgs e)
		{
            StopSynchronous();
            AleaPlayer = null;
		}

		private void UpdateControlsDuringPlay()
		{
			foreach(string lMessage in fMessages)
			{
				edMessages.Text += lMessage + Environment.NewLine;
			}
			fMessages.Clear();
			edMessages.SelectionStart = edMessages.Text.Length;
			edMessages.SelectionLength = 0;
			edMessages.ScrollToCaret();

			UpdateDriveLabels(AleaPlayer.DriveLabels);
		}


		private void lvDriveLabels_SelectedIndexChanged(object sender, System.EventArgs e)
		{
            if (AleaPlayer != null)
    			AleaPlayer.DriveLabels = GetDriveLabels();
		}

        private void TfrmPlayer_Load(object sender, EventArgs e)
        {
            PopulateDriveLabelsList();
        }

        private void StopSynchronous()
        {
            if (AleaPlayer != null)
            {
                AleaPlayer.StopPlaying();
                while (AleaPlayer != null && !AleaPlayer.Stopped)
                {
                    Application.DoEvents(); // let the background thread finish any Invokes!
                }
                Application.DoEvents(); // let the background thread finish any Invokes!
            }
        }

        private void TfrmPlayer_FormClosing(object sender, FormClosingEventArgs e)
        {
            StopSynchronous();
        }
	}
}
